﻿' <summary>
' BeeWit IDE Entorno de desarrollo Visual para php.
' BeeWitPHP IDE es el único entorno de desarrollo para PHP 
' de tipo WYSIWYG (What You See Is What You Get). 
' ¡Ahora si!, aproveche la potencia del lenguaje PHP 
' y desarrolle más rápidamente gracias a la programación 
' intuitiva de BeeWit.
' Copyright © 2010 Dewins Murillo Garcia 
' EMAIL: dewinsmg@gmail.com
' EMAIL: dewins@beewitsoft.com
' Este Programa es Software Libre: usted puede redistribuirlo 
' y/o modificarlo bajo los términos de la Licencia Publica General 
' GNU como es publicada por la Fundacion de Software Libre; 
' en la 3ra versión de la licencia.
' Este programa es distribuido con la esperanza de que sea útil, 
' pero SIN GARANTÍA ALGUNA; sin siquiera la garantía implícita 
' de VALOR COMERCIAL o FORMADO PARA UN PROPÓSITO EN PARTICULAR. 
' Vea la Licencia Publica General GNU para mas detalles.
' Usted debe haber recibido una copia de la Licencia Publica General 
' GNU junto con este programa. Si no, vaya a http://www.beewitsoft.com
' o en http://code.google.com/p/ajax-beewit-php-ide/
' </summary>
' <remarks></remarks>

Public Class ctlExplorer

    Dim ActualNode As clsNode

    Private Sub ctlExplorer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TreeFiles.ImageList = Object_Icons.bmpImageList

        Me.menuDirectoryAdd.Text = Object_Language.Lang_Text("Add")
        Me.MenuDirectoryDelete.Text = Object_Language.Lang_Text("Delete")
        Me.MenuDirectoryMenuRename.Text = Object_Language.Lang_Text("Rename")
        Me.MenuDirectoryRefresh.Text = Object_Language.Lang_Text("Refresh")
        Me.MenuDirectoryCreateDirectory.Text = Object_Language.Lang_Text("Create Directory")
        Me.MenuDirectoryAddUserControl.Text = Object_Language.Lang_Text("User Control")
        Me.MenuDirectoryaddStructuredPHP.Text = Object_Language.Lang_Text("Structured PHP")
        Me.MenuDirectoryAddPHPClass.Text = Object_Language.Lang_Text("PHP Class")
        Me.MenuDirectoryAddOtherFile.Text = Object_Language.Lang_Text("Other File")
        Me.MenuDirectoryAddIndex.Text = Object_Language.Lang_Text("Begin idenx.php")
        Me.MenuDirectoryAddHTMLCode.Text = Object_Language.Lang_Text("HTML Code")
        Me.MenuDirectoryDelete.Image = Object_Icons.bmpImage("icon_delete_directory")
        Me.MenuDirectoryMenuRename.Image = Object_Icons.bmpImage("icon_rename_directory")
        Me.MenuDirectoryRefresh.Image = Object_Icons.bmpImage("icon_refresh")


        Me.menuFileOpen.Text = Object_Language.Lang_Text("Open File")
        Me.MenuFileDelete.Text = Object_Language.Lang_Text("Delete")
        Me.MenuFileRename.Text = Object_Language.Lang_Text("Rename")

        Me.MenuDirectoryCreateDirectory.Image = Object_Icons.bmpImage("icon_create_directory")
        Me.MenuDirectoryAddUserControl.Image = Object_Icons.bmpImage("icon_add_user_control")
        Me.MenuDirectoryaddStructuredPHP.Image = Object_Icons.bmpImage("icon_add_php_estructurado")
        Me.MenuDirectoryAddPHPClass.Image = Object_Icons.bmpImage("icon_add_class")
        Me.MenuDirectoryAddHTMLCode.Image = Object_Icons.bmpImage("icon_add_html")
        Me.MenuDirectoryAddOtherFile.Image = Object_Icons.bmpImage("icon_add_other")
        Me.MenuDirectoryAddIndex.Image = Object_Icons.bmpImage("icon_add_index")

        Me.menuFileOpen.Image = Object_Icons.bmpImage("icon_open_file")
        Me.MenuFileDelete.Image = Object_Icons.bmpImage("icon_delete_file")
        Me.MenuFileRename.Image = Object_Icons.bmpImage("icon_rename_file")
    End Sub

    Public Sub LoadProject()
        Me.TreeFiles.Nodes.Clear()
        Dim NodeRoot As New clsNode()
        NodeRoot.ContextMenuStrip = MenuDirectory
        NodeRoot.Text = Project_Name
        NodeRoot.Path = Project_Path
        NodeRoot.ToolTipText = Project_Path
        NodeRoot.ImageKey = "icon_project"
        NodeRoot.SelectedImageKey = "icon_project"
        LoadDirectories(NodeRoot)
        NodeRoot.Expand()
        Me.TreeFiles.Nodes.Add(NodeRoot)
        Control_Controls.LoadControls()
    End Sub

    Private Sub LoadDirectories(ByRef NodeParent As clsNode)
        Dim ListDir() As ifzFilesManager.strFile = Project_Interfaz.ListDirectories(NodeParent.Path)
        If ListDir Is Nothing Then
            Exit Sub
        End If
        For Each Directory In ListDir
            Dim NewNode As New clsNode
            NewNode.Text = Directory.Name
            NewNode.Path = Directory.Path
            NewNode.ContextMenuStrip = MenuDirectory
            NewNode.ToolTipText = Directory.Path
            NewNode.Tag = "directory"
            NewNode.ImageKey = "icon_directory"
            NodeParent.Nodes.Add(NewNode)
            LoadDirectories(NewNode)
        Next

        Dim ListFiles() As ifzFilesManager.strFile = Project_Interfaz.ListFiles(NodeParent.Path)

        For Each Files In ListFiles
            Dim NewNode As New clsNode
            Dim Extension As String = Files.Name.Substring(Files.Name.LastIndexOf(".") + 1)
            If Extension.ToUpper <> "VIEW" Then
                If Not Files.Name.ToUpper Like "*CJX.GIF*" Then
                    NewNode.Text = Files.Name
                    NewNode.Path = Files.Path
                    NewNode.ContextMenuStrip = MenuFile
                    NewNode.ToolTipText = Files.Path
                    NewNode.Tag = "file"
                    NewNode.ImageKey = "icon_unknow"
                    NewNode.SelectedImageKey = "icon_unknow"
                    If Object_Icons.KeyExists("icon_" & Extension) Then
                        NewNode.ImageKey = "icon_" & Extension
                        NewNode.SelectedImageKey = NewNode.ImageKey
                    End If
                    NodeParent.Nodes.Add(NewNode)
                End If

            End If
        Next

    End Sub

    Private Sub TreeFiles_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeFiles.AfterLabelEdit
        Try
            Dim Node As clsNode = Me.TreeFiles.SelectedNode
            If Not e.Label Is Nothing Then
                If Node.Tag = "directory" Then
                    Dim NewDirectoryName As String = Node.ToolTipText.ToString.Substring(0, Node.ToolTipText.ToString.LastIndexOf("\")) & "\" & e.Label
                    If Not Project_Interfaz.RenameDirectory(e.Node.ToolTipText, NewDirectoryName, True) Then
                        e.CancelEdit = True
                    Else
                        Node.Tag = "directory"
                        e.Node.ToolTipText = NewDirectoryName
                    End If
                End If
                If Node.Tag = "file" Then
                    Dim NewFileName As String = Node.ToolTipText.ToString.Substring(0, Node.ToolTipText.ToString.LastIndexOf("\")) & "\" & e.Label
                    If Not Project_Interfaz.RenameFile(e.Node.ToolTipText, NewFileName, True) Then
                        e.CancelEdit = True
                    Else
                        If Node.TypeCode = clsNode.enumTypeCode.isCONTROL Then
                            Project_Interfaz.RenameFile(e.Node.ToolTipText & ".view", NewFileName & ".view", True)
                        End If
                        Node.ToolTipText = NewFileName
                        If Node.TypeCode = clsNode.enumTypeCode.isCLASS Or Node.TypeCode = clsNode.enumTypeCode.isCONTROL Then
                            Node.ContentCode = Object_Expression.ReplaceClassName(Node.ContentCode, e.Label.Substring(0, e.Label.IndexOf(".")))
                            Project_Interfaz.SetContentFile(Node.ToolTipText, Node.ContentCode)
                        End If
                        Node.Tag = "file"
                        Node.Path = Node.ToolTipText
                    End If
                End If
            End If
        Catch ex As Exception
            e.CancelEdit = True
            MsgBox(ex.Message, MsgBoxStyle.Critical, Object_Language.Lang_Text("Rename"))
        End Try
        Control_Controls.LoadControls()
    End Sub

    Private Sub TreeFiles_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeFiles.MouseDown
        ActualNode = Me.TreeFiles.GetNodeAt(e.X, e.Y)
    End Sub

    Private Sub MenuDirectoryCreateDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryCreateDirectory.Click
        Dim newName As String = Object_Language.Lang_Text("New Directory")
        Dim i As Long = 1
        Dim noFound As Boolean = True
        Do While noFound
            For Each SubDirectory As TreeNode In ActualNode.Nodes
                If SubDirectory.Text = newName Then
                    newName = Object_Language.Lang_Text("New Directory") & " " & i
                    i += 1
                    Continue Do
                End If
            Next
            noFound = False
        Loop
        Dim NewNode As New clsNode()
        NewNode.Text = newName
        NewNode.ToolTipText = ActualNode.ToolTipText & "\" & newName
        NewNode.ImageKey = "icon_directory"
        NewNode.SelectedImageKey = "icon_directory"
        NewNode.Tag = "directory"
        If Project_Interfaz.CreateDirectory(NewNode.ToolTipText, True) Then
            ActualNode.Nodes.Add(NewNode)
            NewNode.Parent.Expand()
            ActualNode = NewNode
            ActualNode.ContextMenuStrip = MenuDirectory
            Me.TreeFiles.SelectedNode = ActualNode
            Me.TreeFiles.SelectedNode.BeginEdit()
        End If
    End Sub

    Private Sub MenuDirectoryMenuRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryMenuRename.Click
        Me.TreeFiles.SelectedNode = ActualNode
        Me.TreeFiles.SelectedNode.BeginEdit()
        Control_Controls.LoadControls()
    End Sub

    Private Sub MenuDirectoryRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryRefresh.Click
        ActualNode.Nodes.Clear()
        LoadDirectories(ActualNode)
    End Sub

    Private Sub MenuDirectoryDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryDelete.Click
        If ActualNode.Tag = "directory" Then
            If MsgBox(Object_Language.Lang_Text("Confirm delete") & " [" & ActualNode.ToolTipText & "]?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, Object_Language.Lang_Text("Deleting")) = MsgBoxResult.Yes Then
                If Project_Interfaz.DeleteDirectory(ActualNode.ToolTipText, True) Then
                    ActualNode.Remove()
                End If
            End If
        Else

        End If
    End Sub

    Private Sub MenuFileDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileDelete.Click
        If ActualNode.Tag = "file" Then
            If MsgBox(Object_Language.Lang_Text("Confirm delete") & " [" & ActualNode.ToolTipText & "]?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, Object_Language.Lang_Text("Deleting")) = MsgBoxResult.Yes Then
                If Project_Interfaz.DeleteFile(ActualNode.ToolTipText, True) Then
                    If ActualNode.Extention = "CJX" Then
                        Project_Interfaz.DeleteFile(ActualNode.ToolTipText & ".view", True)
                    End If
                    ActualNode.Remove()
                End If
            End If
        End If
        Control_Controls.LoadControls()
    End Sub

    Private Sub MenuFileRename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuFileRename.Click
        Me.TreeFiles.SelectedNode = ActualNode
        Me.TreeFiles.SelectedNode.BeginEdit()
        Control_Controls.LoadControls()
    End Sub

    Private Sub TreeFiles_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeFiles.NodeMouseDoubleClick
        If Not ActualNode Is Nothing Then
            If ActualNode.Tag = "file" Then
                Control_Main.AddEditor(ActualNode.ToolTipText, Me.TreeFiles.GetNodeAt(e.X, e.Y))
                Control_Console.AddMessages("Opened! " & ActualNode.ToolTipText & ". ", ctlConsole.enumTypeMessages.isINFORMATION)
            End If
        End If
    End Sub

    Public Function SearchClassbyFileName(ByVal FileName As String, Optional ByRef NodeParent As clsNode = Nothing) As clsNode
        Dim NodeFound As clsNode
        If NodeParent Is Nothing Then
            NodeParent = Me.TreeFiles.Nodes(0)
        End If
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "file" Then
                If FileName.ToUpper = Node.ToolTipText.ToUpper Then
                    Return Node
                End If
            End If
        Next
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "directory" Then
                NodeFound = SearchClassbyFileName(FileName, Node)
                If Not NodeFound Is Nothing Then
                    Return NodeFound
                End If
            End If
        Next
        Return Nothing
    End Function

    Public Function SearchClass(ByVal ClassName As String, Optional ByRef NodeParent As clsNode = Nothing) As clsNode
        Dim NodeFound As clsNode
        If NodeParent Is Nothing Then
            NodeParent = Me.TreeFiles.Nodes(0)
        End If
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "file" Then
                If Node.Extention = "CJX" Or Node.Extention = "CLS" Then
                    Dim Content As String = Node.ContentCode
                    Dim ClassNameFound As String = Object_Expression.GetClassName(Content)
                    If ClassName = ClassNameFound Then
                        Return Node
                    End If
                End If
            End If
        Next
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "directory" Then
                NodeFound = SearchClass(ClassName, Node)
                If Not NodeFound Is Nothing Then
                    Return NodeFound
                End If
            End If
        Next
        Return Nothing
    End Function

    Public Function SearchClasses(Optional ByRef NodeParent As clsNode = Nothing) As clsNode()
        Dim NodeFound() As clsNode = Nothing
        Dim iRow As Long = -1
        If NodeParent Is Nothing Then
            NodeParent = Me.TreeFiles.Nodes(0)
        End If
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "file" Then
                If Node.Extention = "CJX" Or Node.Extention = "CLS" Then
                    Dim Content As String = Node.ContentCode
                    Dim ClassNameFound As String = Object_Expression.GetClassName(Content)
                    If ClassNameFound <> "" Then
                        iRow += 1
                        ReDim Preserve NodeFound(iRow)
                        NodeFound(iRow) = Node
                    End If
                End If
            End If
        Next
        For Each Node As clsNode In NodeParent.Nodes
            If Node.Tag = "directory" Then
                Dim OtherNodesFound() As clsNode = SearchClasses(Node)
                If Not OtherNodesFound Is Nothing Then
                    For Each OtherNode As clsNode In OtherNodesFound
                        iRow += 1
                        ReDim Preserve NodeFound(iRow)
                        NodeFound(iRow) = OtherNode
                    Next
                End If
            End If
        Next
        Return NodeFound
    End Function


    Public Sub CopyTemplate(ByVal Extension As String, ByVal TypeCode As clsNode.enumTypeCode)
        Dim BaseName As String = "noname_@var@." & Extension
        Dim NewName As String = "noname_1." & Extension
        Dim iName As Long = 1
        Dim NoFound As Boolean = True
        Dim NewNode As New clsNode()
        Do While NoFound
            For Each Files As clsNode In ActualNode.Nodes
                NewName = BaseName.Replace("@var@", iName)
                If Files.Text = NewName Then
                    iName += 1
                    Continue Do
                End If
            Next
            NoFound = False
        Loop
        NewNode.Text = NewName
        NewNode.Extention = Extension.ToUpper
        NewNode.ToolTipText = ActualNode.ToolTipText & "\" & NewName
        NewNode.ImageKey = "icon_" & Extension
        NewNode.SelectedImageKey = "icon_" & Extension
        NewNode.Tag = "file"
        NewNode.TypeCode = TypeCode
        NewNode.ContentCode = ""
        NewNode.ContentView = ""
        If Project_Interfaz.CreateFile(NewNode.ToolTipText, True) Then
            Dim NewContent As String = General.Object_LocalFiles.GetContentFile(Application.StartupPath & "\resources\templates\template." & Extension)
            NewContent = NewContent.Replace(" noname ", " " & NewName.Replace("." & Extension, "") & " ")
            NewNode.ContentCode = NewContent 'CONTENT CODE
            Project_Interfaz.SetContentFile(NewNode.ToolTipText, NewContent)
            ActualNode.Nodes.Add(NewNode)
            If TypeCode = clsNode.enumTypeCode.isCONTROL Then
                Dim NewContentView As String = General.Object_LocalFiles.GetContentFile(Application.StartupPath & "\resources\templates\template." & Extension & ".view")
                Project_Interfaz.CreateFile(NewNode.ToolTipText & ".view", True)
                Project_Interfaz.SetContentFile(NewNode.ToolTipText & ".view", NewContentView)
                NewNode.ContentView = NewContentView 'CONTENT VIEW
            End If
            NewNode.Parent.Expand()
            NewNode.Path = NewNode.ToolTipText
            ActualNode = NewNode
            ActualNode.ContextMenuStrip = MenuFile
            ActualNode.BeginEdit()
            Me.TreeFiles.SelectedNode = ActualNode
        End If
    End Sub


    Private Sub MenuDirectoryaddStructuredPHP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryaddStructuredPHP.Click
        CopyTemplate("php", clsNode.enumTypeCode.isPHP)
    End Sub


    Private Sub MenuDirectoryAddUserControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryAddUserControl.Click
        CopyTemplate("cjx", clsNode.enumTypeCode.isCONTROL)
    End Sub

    Private Sub MenuDirectoryAddPHPClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryAddPHPClass.Click
        CopyTemplate("cls", clsNode.enumTypeCode.isCLASS)
    End Sub

    Private Sub MenuDirectoryAddHTMLCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryAddHTMLCode.Click
        CopyTemplate("html", clsNode.enumTypeCode.isHTML)
    End Sub

    Private Sub MenuDirectoryAddIndex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDirectoryAddIndex.Click
        General.Project_Interfaz.CopyFile(Application.StartupPath & "\resources\templates\index.php", Project_Path & "\index.php", False)
        General.Project_Interfaz.CopyFile(Application.StartupPath & "\resources\templates\index.cjx", Project_Path & "\index.cjx", False)
        General.Project_Interfaz.CopyFile(Application.StartupPath & "\resources\templates\index.cjx.view", Project_Path & "\index.cjx.view", False)
        Me.TreeFiles.Nodes.Clear()
        LoadProject()
    End Sub

    Private Sub MenuDirectoryAddOtherFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuDirectoryAddOtherFile.Click
        CopyTemplate("txt", clsNode.enumTypeCode.isOTHER)
    End Sub

    Private Sub TreeFiles_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeFiles.AfterSelect

    End Sub
End Class
